﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

using CustomsSystem.BLL;
using CustomsSystem.Model;
using Maticsoft.Common;
namespace WEB.T_document
{
    public partial class modelset : System.Web.UI.Page
    {
        public static int stepIndex = 1;
        public static DataTable dt;
        public CodeManage codebll = new CodeManage();
        CustomsSystem.Model.info_document model = new CustomsSystem.Model.info_document();
        CustomsSystem.BLL.info_document DocBLL = new CustomsSystem.BLL.info_document();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //dt = new DataTable();
                gvDocument.DataSource = dt;
                gvDocument0.DataSource = codebll.GetList(" codetype ='doctype'");
                gvDocument0.DataBind();

                if (gvDocument.DataSource == null)
                {
                    dt = new DataTable();
                    //============================================
                    dt.Columns.Add("codename", Type.GetType("System.String"));
                    gvDocument.DataSource = dt;
                    gvDocument.DataBind();
                }
                if (gvDocument.DataSource != null)
                {
                    gvDocument.DataSource = null;
                    gvDocument.DataBind();
                    dt = new DataTable();
                    //============================================
                    dt.Columns.Add("codename", Type.GetType("System.String"));
                    gvDocument.DataSource = dt;
                    gvDocument.DataBind();
                }
            }
        }


        protected void gvDocument_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if ((e.Row.RowState == (DataControlRowState.Edit | DataControlRowState.Alternate)) || (e.Row.RowState == DataControlRowState.Edit))
            {
                TextBox curText;
#warning 这里写死了，要注意下
                for (int i = 0; i < 1; i++) // 
                {
                    curText = (TextBox)e.Row.Cells[i].Controls[0];
                    curText.Width = Unit.Pixel(60);
                    if (i == 0) curText.Enabled = false;
                }
            }

        }

        protected void gvDocument_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            dt.Rows.Remove(dt.Rows[e.RowIndex]);
            gvDocument.DataSource = dt;
            gvDocument.DataBind();
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            CustomsSystem.Model.info_documentMastert DM = new CustomsSystem.Model.info_documentMastert();
            CustomsSystem.BLL.info_documentMastert BLLdm = new CustomsSystem.BLL.info_documentMastert();
            if(TextBox1.Text.Trim()!="")
            {
                if(gvDocument.Rows.Count>0)
                {
                    if (BLLdm.Equals(TextBox1.Text) == false)
                    {
                        DM.docViewName = TextBox1.Text;
                        DM.inout = Convert.ToInt16(DropDownList1.SelectedValue);
                        int MastertID = BLLdm.Add(DM);

                        int i;
                        for (i = 0; i < gvDocument.Rows.Count; i++)
                        {
                            model = new CustomsSystem.Model.info_document();

                            model.docViewID = MastertID;
                            if (dt.Rows[i]["codename"].ToString() == "")
                                model.codeName = null;
                            else
                                model.codeName = dt.Rows[i]["codename"].ToString();

                            DocBLL.Add(model);
                        }

                        if (i == gvDocument.Rows.Count)
                        {
                            MessageBox.ShowAndRedirect(this, "设置成功", "modelView.aspx");
                        }
                        else
                        {
                            MessageBox.Show(this, "设置失败!");
                        }
                    }
                }
                    else MessageBox.Show(this, "请添加单证");
                }
                else 
                MessageBox.Show(this, "请输入模板名称");
            }

        protected void Button3_Click(object sender, EventArgs e)
        {
            Response.Redirect("modelView.aspx");
        }

        protected void gvDocument0_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            DataRow dr = dt.NewRow();
            string index = e.CommandArgument.ToString(); //那行被选中,取出选中行的索引 


            dr["codeName"] = gvDocument0.Rows[Convert.ToInt32(e.CommandArgument.ToString())].Cells[0].Text;
            dt.Rows.Add(dr);
            dt.AcceptChanges();
            gvDocument.DataSource = dt;
            gvDocument.DataBind();
        }
    }
}